package com.zyl.pts.config;

import com.zyl.pts.web.inteceptor.AuthInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @version v1.0
 * @ProjectName: SpringBootWeb
 * @Package: com.zyl.demo.config
 * @ClassName: SpringWebConfig
 * @Description TODO()
 * @Author 翟玉龙
 * @Date 2024/11/13-17:07
 */
@Configuration
public class SpringWebConfig implements WebMvcConfigurer {

    @Resource
    private AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 将认证拦截器注册到MVC组件中
        registry.addInterceptor(authInterceptor)
                // 设置需要拦截器拦截的路径
                .addPathPatterns("/**")
                .excludePathPatterns("/noLogin/**",
                        "/login", "/logout",
                        "/profile/**",
                        "/common/download**",
                        "/common/download/resource**",
                        "/swagger-ui.html",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/*/api-docs/**",
                        "/druid/**",
                        "/favicon.ico",
                        "/doc.html");
    }

    // 重写addCorsMappings
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 给所有的资源路径都允许跨域， /** 表示所有路径
        registry.addMapping("/**")
                // 允许来自所有网站的跨域请求
                .allowedOrigins("*")
                // 允许所有请求方法
                .allowedMethods("*")
                // 允许所有请求头
                .allowedHeaders("*");

    }

}
